package com.csd.fragment;

import java.util.Timer;
import java.util.TimerTask;

import com.csd.activity.PersonalDetailActivity;
import com.csd.activity.PersonalOrderActivity;
import com.csd.activity.R;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ReturnCarFragment extends Fragment implements OnClickListener {

	private int minute;
	private int second;
	private int hour;
	private TextView tv_hour2, tv_minute2, tv_second2;
	private TimerTask timerTask2;
	private Timer timer2;
	private View v;
	private Button bt_more, bt_returnCar, bt_lock, bt_door, bt_bugle;
	Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			if (msg.what == 0) {
				if (hour == 0 && minute == 0 && second == 0) {
					if (hour >= 10) {
						tv_hour2.setText(String.valueOf(hour));
					} else {
						tv_hour2.setText("0" + String.valueOf(hour));
					}
					if (minute >= 10) {
						tv_minute2.setText(String.valueOf(minute));
					} else {
						tv_minute2.setText("0" + String.valueOf(minute));
					}
					if (second >= 10) {

						tv_second2.setText(String.valueOf(second));
					} else {
						tv_second2.setText("0" + String.valueOf(second));
					}
					return;
				}
				if (second == 0 && minute == 0 && hour != 0) {
					second = 59;
					minute = 59;
					hour -= 1;
				} else if (second == 0 && hour == 0 && minute != 0) {
					second = 59;
					minute -= 1;
				} else if (second == 0 && hour != 0 && minute != 0) {

					second = 59;
					minute -= 1;
				} else {
					second--;
				}

				if (hour >= 10) {
					tv_hour2.setText(String.valueOf(hour));
				} else {
					tv_hour2.setText("0" + String.valueOf(hour));
				}
				if (minute >= 10) {
					tv_minute2.setText(String.valueOf(minute));
				} else {
					tv_minute2.setText("0" + String.valueOf(minute));
				}
				if (second >= 10) {

					tv_second2.setText(String.valueOf(second));
				} else {
					tv_second2.setText("0" + String.valueOf(second));
				}
			}

		};
	};

	@Override
	public View onCreateView(LayoutInflater inflater,
			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		v = inflater.inflate(R.layout.fragment_returncar, null);
		tv_hour2 = (TextView) v.findViewById(R.id.hour);
		tv_minute2 = (TextView) v.findViewById(R.id.minute);
		tv_second2 = (TextView) v.findViewById(R.id.second);
		bt_more = (Button) v.findViewById(R.id.return_more);
		bt_more.setOnClickListener(this);
		bt_lock = (Button) v.findViewById(R.id.lock);
		bt_door = (Button) v.findViewById(R.id.door);
		bt_bugle = (Button) v.findViewById(R.id.bugle);
		bt_lock.setOnClickListener(this);
		bt_door.setOnClickListener(this);
		bt_bugle.setOnClickListener(this);
		bt_returnCar = (Button) v.findViewById(R.id.bt_returncar);
		bt_returnCar.setOnClickListener(this);
		init();
		return v;
	}

	private void init() {

		hour = 0;
		minute = 10;
		second = 0;

		tv_hour2.setText(String.valueOf(hour));
		tv_minute2.setText(String.valueOf(minute));
		tv_second2.setText(String.valueOf(second));

		timerTask2 = new TimerTask() {

			@Override
			public void run() {
				Message msg = new Message();
				msg.what = 0;
				handler.sendMessage(msg);
			}
		};

		timer2 = new Timer();
		timer2.schedule(timerTask2, 0, 1000);

	}

	@Override
	public void onDestroyView() {
		// TODO Auto-generated method stub

		if (timer2 != null) {
			timer2.cancel();
			timer2 = null;
		}
		if (timerTask2 != null) {
			timerTask2 = null;
		}
		minute = -1;
		second = -1;
		hour = -1;
		super.onDestroyView();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.more:
			Intent intent = new Intent(getActivity(),
					PersonalDetailActivity.class);
			startActivity(intent);
			break;
		case R.id.lock:
			break;
		case R.id.door:
			showDoorDialog();
			break;
		case R.id.bugle:
			break;
		case R.id.bt_returncar:
			showReturnCarDialog();
			break;
		default:
			break;
		}
	}
	public void showDoorDialog() {
		// TODO Auto-generated method stub
		final Dialog dialog1 = new Dialog(getActivity(), R.style.Dialog);
		// 设置它的ContentView
		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.dailog_door, null);
		dialog1.setContentView(v);
		TextView tv_sure = (TextView) v.findViewById(R.id.lock_sure);
		TextView tv_cancle = (TextView) v.findViewById(R.id.lock_cancle);
		tv_sure.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog1.dismiss();
			}
		});
		tv_cancle.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog1.dismiss();
			}
		});
		dialog1.show();
	}
	
	public void showReturnCarDialog() {
		// TODO Auto-generated method stub
		final Dialog dialog = new Dialog(getActivity(), R.style.Dialog);
		// 设置它的ContentView
		View v = LayoutInflater.from(getActivity()).inflate(
				R.layout.dailog_hint, null);
		dialog.setContentView(v);
		Button bt_lock = (Button) v.findViewById(R.id.bt_lock);
		bt_lock.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dialog.dismiss();
			}
		});
		dialog.show();
	}
}
